package event

import (
	"beergo/app/check/models"
	"beergo/lib/event"
	"github.com/beego/beego/v2/server/web/context"
)

func init() {
	UpdateQuestionCount()
}

func UpdateQuestionCount() {
	event.ListenJoin("check.BeforeQuestionAnswersCreate", func(v interface{}, ctx *context.Context) {
		md,ok:=v.(*models.QuestionAnswers)
		if ok {
			md.Load("question")
			question := md.Question
			question.Load("answers")
			question.ReplyCount = len(question.Answers)
			question.SetEventOff()
			question.Save()
		}
	})
}